package top.xiaoshuaila.searescue.mqtt;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.xiaoshuaila.searescue.serialport.utils.ByteUtils;
import top.xiaoshuaila.searescue.service.MqttDataDeal;
import top.xiaoshuaila.searescue.service.MqttDataDeal2;
import top.xiaoshuaila.searescue.service.MqttService;

import java.util.Arrays;

@Component
public class MessageCallback implements MqttCallback {
    @Autowired
    private MqttDataDeal2 mqttDataDeal;
    @Override
    public void connectionLost(Throwable throwable) {
        // 连接丢失后，一般在这里面进行重连
        System.out.println("连接断开，可以做重连");
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) {
        // subscribe后得到的消息会执行到这里面
        System.out.println("接收消息主题 : " + topic);
        System.out.println("接收消息Qos : " + message.getQos());
        String str=message.toString().replace(" ","");
        System.out.println("接收消息内容 : " +str);
        try {
            str=str.replaceAll(":","");
            str=str.replaceAll("[\\s\\n\\t:]","");
            mqttDataDeal.detail(ByteUtils.hexStr2Int(str));

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /*此处定义发送消息后的操作*/
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
       // System.out.println("deliveryComplete---------" + token.isComplete());
    }
}
